package pers.csw.lookBook.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import pers.csw.lookBook.mapper.BookRackMapper;
import pers.csw.lookBook.mapper.UserMapper;
import pers.csw.lookBook.common.Res;
import pers.csw.lookBook.common.constant.ErrorCode;
import pers.csw.lookBook.entity.BookRack;
import pers.csw.lookBook.entity.User;
import pers.csw.lookBook.service.UserService;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author csw
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    final UserMapper userMapper;
    final BookRackMapper bookRackMapper;

    public UserServiceImpl(UserMapper userMapper, BookRackMapper bookRackMapper) {
        this.userMapper = userMapper;
        this.bookRackMapper = bookRackMapper;
    }


    @Override
    public Res register(User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        System.out.println(user);
        User user1 = userMapper.selectOne(wrapper);
        if(user1!=null){
            return Res.fail(ErrorCode.USER_NAME_EXIST);
        }else{
            userMapper.insert(user);
            return Res.success(user);
        }

    }

    @Override
    public Res login(String username, String password){
        Map<String, String> map = new HashMap<String, String>();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userMapper.selectOne(wrapper);
        if(!Objects.isNull(user)){
            if(user.getUsername().equals(username)){
                if(!user.getPassword().equals(password)){
                    return Res.fail(ErrorCode.USER_PASSWORD_ERROR);
                }
            }else{
                return Res.fail(ErrorCode.USER_PASSWORD_ERROR);
            }
        }else{
            return Res.fail(ErrorCode.USER_ACCOUNT_NOT_EXIST);
        }
        StpUtil.login(user.getId());
        map.put("username",user.getUsername());
        map.put("userImg",user.getImgUrl());
        map.put("userId",String.valueOf(user.getId()));
        map.put("token",StpUtil.getTokenValue());
        return Res.success(map);
    }

    @Override
    public Res getUserInfo(Integer id){
        return Res.success(userMapper.selectById(id));
    }

    @Override
    public Res updateUser(User user) {
        return Res.success(userMapper.updateById(user));
    }

    @Override
    public Res getBookRackByUserId(Integer userId) {
        QueryWrapper<BookRack> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return Res.success(bookRackMapper.selectList(wrapper));
    }



}
